-
Notifications
You must be signed in to change notification settings - Fork 10.6k
SE-0492: Metatypes and existential metatypes #85139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
DougGregor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach looks reasonable to me. I do really like having this feature, but it's a reasonable basis for any kind of "type registry" in the language
SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift
Outdated
Show resolved
Hide resolved
| @section("mysection") let metatype2: Any.Type = Int.self | ||
| @section("mysection") let metatype3: Any.Type = S.self | ||
| @section("mysection") let metatype4: any (Hashable & Sendable).Type = Int.self | ||
| @section("mysection") let metatype5: any (Hashable & Sendable).Type = S.self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for bound generic types, e.g. Array<Int>.self?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're rejecting that case syntactically: https://github.com/swiftlang/swift/pull/85139/files#diff-ea7de05f211d676ea604c35f0e6b253fb4a454efbfcc84928a931bab113e7bdcR82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...but I did add that as a SIL test into test/SILOptimizer/static_init_metatypes.swift.
a27abcc to
16ebf84
Compare
|
@swift-ci please test |
|
And to point out explicitly: The OptUtils.swift and IRGen changes now cause MetatypeInst and InitExistentialMetatypeInst to be constant folded and statically initialized even outside of |
| @@ -0,0 +1,57 @@ | |||
| // RUN: %target-swift-frontend %s -parse-as-library -module-name=test -emit-sil | %FileCheck %s | |||
| // RUN: %target-swift-frontend %s -parse-as-library -O -module-name=test -emit-sil | %FileCheck %s | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add a -emit-ir invocation to make sure that IRGen can deal with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that reasonable
Yes, that should be fine. As long as IRGen can handle those SIL initializer instructions.
|
https://ci.swift.org/job/swift-PR-macos/25548/console https://ci.swift.org/job/swift-PR-Linux/24711/console https://ci-external.swift.org/job/swift-PR-windows/47464/console I think those are all real issues caused by the new constant folding actually adding these referencing to the witness tables. @eeckstein any ideas here? |
|
Difficult to say without looking at the generated llvm ir. Maybe it's a problem if the referenced type is defined in a different module |
SE-0492 declared that constant expressions should support metatypes (of non-generic, non-resilient types). This adds support for using metatypes (T.self) and existential metatypes (upcasts to Any.Type, etc.) as static initializers for global variables, including those with @section attributes.
Major parts: